package com.example.orderapplication.helper

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Environment
import android.util.Log
import com.example.orderapplication.R
import com.example.orderapplication.bean.UserData
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.util.*


class DataHelper(context: Context?) :
    SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
    private val context: Context? = context
    private val TAG = "DataHelper"
    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(UserData.CREATE_TABLE)

    }

    /**
     * 更新数据库
     */
    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        db.execSQL("DROP TABLE IF EXISTS " + UserData.TABLE_NAME)
        onCreate(db)
    }

    /**
     * 新增
     *
     * @param userData
     */
    fun insertFilmUserData(userData: UserData): Boolean {
        return try {
            val db = this.writableDatabase
            val values = ContentValues()
            values.put(UserData.PASSWORD, userData.password)
            values.put(UserData.PHONE, userData.phone)
            values.put(UserData.NAME, userData.name)
            db.insert(UserData.TABLE_NAME, null, values)
            db.close()
            true
        } catch (e: Exception) {
            false
        }
    }

    /**
     * 查询
     *
     * @param text
     * @return
     */
    fun queryUser(text: String): ArrayList<UserData> {
        val userDataArrayList: ArrayList<UserData> = ArrayList<UserData>()
        val selectQuery =
            "SELECT * FROM " + UserData.TABLE_NAME + " where " + UserData.PHONE + "=" + text
        val db = readableDatabase

        @SuppressLint("Recycle")
        val cursor = db.rawQuery(selectQuery, null)
        if (cursor != null) {
            while (cursor.moveToNext()) {
                val businessCard = UserData()
                val id = cursor.getInt(cursor.getColumnIndex(UserData.ID))
                val password = cursor.getString(cursor.getColumnIndex(UserData.PASSWORD))
                val phone = cursor.getString(cursor.getColumnIndex(UserData.PHONE))
                val name = cursor.getString(cursor.getColumnIndex(UserData.NAME))
                businessCard.id = id
                businessCard.password = password
                businessCard.phone = phone
                businessCard.name = name
                userDataArrayList.add(businessCard)
            }
        }
        db.close()
        return userDataArrayList
    }

    companion object {
        private const val DATABASE_VERSION = 1
        private val DATABASE_NAME =
            Environment.getExternalStorageDirectory().toString() + "/db/user_data.db"
    }

    private fun bitmabToBytes(context: Context?, tmp: Int): ByteArray? {
        Log.e(TAG, tmp.toString())
        //将图片转化为位图
        val bitmap = BitmapFactory.decodeResource(context!!.resources, tmp)
        val size = bitmap.width * bitmap.height * 4
        //创建一个字节数组输出流,流的大小为size
        val baos = ByteArrayOutputStream(size)
        try {
            //设置位图的压缩格式，质量为100%，并放入字节数组输出流中
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
            //将字节数组输出流转化为字节数组byte[]
            return baos.toByteArray()
        } catch (e: Exception) {
        } finally {
            try {
                bitmap.recycle()
                baos.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return ByteArray(0)
    }
}
